﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using CashFlow.Db.Entity;
using CashFlow.Db.DAL;
using CashFlow.Web.Areas.ShopManage.Service;
using CashFlow.Web.Areas.ShopManage.Models.Appointment.Result;

namespace CashFlow.Web.Areas.ShopManage.Service.Implement
{
    public class AppointmentService
        : MyService<AppointmentEntity>, IAppointmentService
    {
        public IList<AppointmentUserInfoModel> QueryByWhere(
            int shopManagerUserId
            , string date
            , string name)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<AppointmentEntity>()
                              .InnerJoin<UserInfoEntity>((a, u) => a.UserId == u.Id)
                              .InnerJoin<ShopUserEntity>((a, u, su) => a.UserId == su.UserId)
                              .Where((a, u, su) => su.ShopManagerUserId == shopManagerUserId
                                    && a.Time.ToString("yyyy-MM-dd") == date);

                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where((a, u, su) => u.Name.Contains(name));
                }

                var ls = query.OrderByDescending((a, u, su) => a.Time)
                              .Select((a, u, su) => new AppointmentUserInfoModel
                              {
                                  Name = u.Name,
                                  InvitationCode = u.InvitationCode,
                                  Phone = u.Phone,
                                  Type = a.Type,
                                  Time = a.Time
                              })
                              .ToList();
                return ls;
            }
        }
    }
}